<?php

namespace Medicina\BackendBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Medicina\KernelBundle\Entity\Catedra;
use Medicina\BackendBundle\Form\CatedraType;
use Medicina\BackendBundle\Form\CatedraSearchType;

/**
 * Catedra controller.
 *
 */
class CatedraController extends Controller {

    /**
     * Lists all Catedra entities.
     *
     */
    public function indexAction() {

//        $usr = $this->get('security.context')->getToken()->getUser();
//        die($usr->getId());
//        
        $entity = new Catedra();
        $searchForm = $this->createForm(new CatedraSearchType(), $entity);
        $catedras = new \Doctrine\Common\Collections\ArrayCollection();
        $paginador = $this->get('ideup.simple_paginator');
        $paginador->setItemsPerPage(10);
        $em = $this->getDoctrine()->getEntityManager();
        $session = $this->get('session');
        $session->set('s_isSearch', false);
        $request = $this->getRequest();
        $filter = false;
        if ($request->getMethod() === 'POST') {
            $searchForm->bindRequest($request);
            //   if ($searchForm->isValid()) {
            $session->set('s_isSearch', true);
            $searchCriteria = $searchForm->getData();
            $nombre = $searchCriteria->getNombre();
            $catedras = $em->getRepository('MedicinaKernelBundle:Catedra')->getCatedraByCriteria($nombre);
            $filter = true;
            //   } else {
            //       $catedras = $em->getRepository('MedicinaKernelBundle:Catedra')->findAll();
            //   }
        } else {
            $catedras = $em->getRepository('MedicinaKernelBundle:Catedra')->findAll();
        }

        $entities = $paginador->paginate($catedras)->getResult();
        return $this->render('BackendBundle:Catedra:index.html.twig', array(
                    'entities' => $entities,
                    'paginador' => $paginador,
                    'searchForm' => $searchForm->createView(),
                    'filter' => $filter,
                    'delete_form' => $this->createDeleteForm()->createView()
                ));
    }

    /**
     * Finds and displays a Catedra entity.
     *
     */
    public function showAction($id) {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('MedicinaKernelBundle:Catedra')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('No se ha encontrado la catedra solicitada');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('BackendBundle:Catedra:show.html.twig', array(
                    'entity' => $entity,
                    'delete_form' => $deleteForm->createView(),
                ));
    }

    /**
     * Displays a form to create a new Catedra entity.
     *
     */
    public function newAction() {
        $entity = new Catedra();
        $form = $this->createForm(new CatedraType(), $entity);

        return $this->render('BackendBundle:Catedra:new.html.twig', array(
                    'entity' => $entity,
                    'form' => $form->createView()
                ));
    }

    /**
     * Creates a new Catedra entity.
     *
     */
    public function createAction() {
        $entity = new Catedra();
        $request = $this->getRequest();
        $form = $this->createForm(new CatedraType(), $entity);
        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            try {
                $em->persist($entity);
                $em->flush();

                return $this->redirect($this->generateUrl('catedra', array('id' => $entity->getId())));
            } catch (\PDOException $e) {
                $this->get('session')->setFlash('error', 'Ya existe una catedra con ese código y/o nombre');
                // return $this->redirect($this->generateUrl('cargo_new'));
                return $this->render('BackendBundle:Catedra:new.html.twig', array(
                            'entity' => $entity,
                            'form' => $form->createView()
                        ));
            }
        }

        $this->get('session')->setFlash('error', 'Ya existe una catedra con ese código y/o nombre');
        // return $this->redirect($this->generateUrl('cargo_new'));
        return $this->render('BackendBundle:Catedra:new.html.twig', array(
                    'entity' => $entity,
                    'form' => $form->createView()
                ));
    }

    /**
     * Displays a form to edit an existing Catedra entity.
     *
     */
    public function editAction($id) {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('MedicinaKernelBundle:Catedra')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('No se ha encontrado la catedra solicitada');
        }

        $editForm = $this->createForm(new CatedraType(), $entity);

        return $this->render('BackendBundle:Catedra:edit.html.twig', array(
                    'entity' => $entity,
                    'edit_form' => $editForm->createView(),
                ));
    }

    /**
     * Edits an existing Catedra entity.
     *
     */
    public function updateAction($id) {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('MedicinaKernelBundle:Catedra')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('No se ha encontrado la catedra solicitada');
        }

        $editForm = $this->createForm(new CatedraType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        $request = $this->getRequest();

        $editForm->bindRequest($request);

        if ($editForm->isValid()) {
            try {
                $em->persist($entity);
                $em->flush();

                return $this->redirect($this->generateUrl('catedra', array('id' => $id)));
            } catch (\PDOException $e) {
                $this->get('session')->setFlash('error', 'Ya existe una catedra con ese código y/o nombre');

                return $this->render('BackendBundle:Catedra:edit.html.twig', array(
                            'entity' => $entity,
                            'edit_form' => $editForm->createView(),
                            'delete_form' => $deleteForm->createView(),
                        ));
            }
        }
        $this->get('session')->setFlash('error', 'Ya existe una catedra con ese código y/o nombre');

        return $this->render('BackendBundle:Catedra:edit.html.twig', array(
                    'entity' => $entity,
                    'edit_form' => $editForm->createView(),
                    'delete_form' => $deleteForm->createView(),
                ));
    }

    public function deleteAction($id) {
        $em = $this->getDoctrine()->getEntityManager();
        $entity = $em->getRepository('MedicinaKernelBundle:Catedra')->find($id);

        if (!$entity) {
            $ $this->get('session')->setFlash('error', 'No se encontro la catedra');
        } else {
            try {
                $distinguible = $entity->getNombre();
                $em->remove($entity);
                $em->flush();
                $this->get('session')->setFlash('notice', 'Catedra   ' . $distinguible . ' borrada con exito');
            } catch (\PDOException $e) {
                $this->get('session')->setFlash('error', 'No se puede borrar la catedra ' . $distinguible . ', esta siendo usada por un concurso o un cargo');
            }
        }
        return $this->redirect($this->generateUrl('catedra'));
    }

    private function createDeleteForm() {
        return $this->createFormBuilder()->getForm();
    }

    public function getCargosAction($id) {
        $httpKernel = $this->container->get('http_kernel');
        $response = $httpKernel->forward(
                'BackendBundle:Cargo:getByCatedra', array(
            "catedraId" => $id,
                )
        );
        return $response;
    }

    public function getConcursosAction($id) {
        $httpKernel = $this->container->get('http_kernel');
        $response = $httpKernel->forward(
                'BackendBundle:Concurso:getByCatedra', array(
            "catedraId" => $id,
                )
        );
        return $response;
    }

}
